﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace objass
{
    /// <summary>
    /// Edit2Rel.xaml 的交互逻辑
    /// </summary>
    public partial class EditRel : Window
    {
        // 新增公共属性，用于接收节点信息
        public string NodeNameA { get; set; }
        public List<string> NodeNameB{ get; private set; } = new List<string>();

        public EditRel()
        {
            InitializeComponent();
        }

        // 在窗口加载时，将 NodeInfo 的值赋给 ListBox
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (NodeNameA != null)
            {
                NodeNameLabel1.Content = NodeNameA;
            }
            var app = (App)Application.Current;
            var rels = app.GetRelTypes();
            wordSelector.ItemsSource = rels;

            if (NodeNameB.Count() >= 1 && !string.IsNullOrEmpty(NodeNameB[0]))
            {
                NodeNameLabel2.Text = GetWords(NodeNameB);
                var relName = app.GetRelName(NodeNameA, NodeNameB[0]);
                var revert = app.IsRelTypeRevert(relName);
                var relType = relName;
                if (revert)
                {
                    relType = app.SwapParts(relType);
                    CheckReverse.IsChecked = true;
                }
                
                RelNameLabel.Content = relName;

                // 找到 relName 对应的项并选中
                for (int i = 0; i < wordSelector.Items.Count; i++)
                {
                    if (wordSelector.Items[i].ToString() == relType)
                    {
                        wordSelector.SelectedIndex = i;
                        wordSelector.ScrollIntoView(wordSelector.Items[i]); // 确保选中的项在可视化界面里
                        break;
                    }
                }
            }
        }

        private string GetWords(List<string> nodes)
        {
            var words = "";
            foreach (var word in nodes)
            {
                words += word + ", ";
            }
            char[] charsToTrim = { ',', ' ' };
            words = words.TrimEnd(charsToTrim);
            return words;
        }

        private void SelectNodeButton_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new NodesInput();
            if (dialog.ShowDialog() == true)
            {
                //剔除节点A
                dialog.SelectedNodes.Remove(NodeNameA);
                // 处理选中的节点
                NodeNameLabel2.Text = GetWords(dialog.SelectedNodes);
                NodeNameB = dialog.SelectedNodes;
                RelNameLabel.Content = "";
                wordSelector.SelectedIndex = -1;
                CheckReverse.IsChecked = false;
            }
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            var app = (App)Application.Current;
            if(!string.IsNullOrEmpty(NodeNameA)
                && !string.IsNullOrEmpty(RelNameLabel.Content.ToString()))
            {
                foreach (var word in NodeNameB)
                {
                    app.SaveRelName(NodeNameA, word, RelNameLabel.Content.ToString());
                }
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                MessageBox.Show("节点1,节点2,关系名称不能为空");
                return;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void WordSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // 处理 wordSelector 的选项变化事件
            if (wordSelector.SelectedItem != null)
            {
                var app = (App)Application.Current;
                var relname = wordSelector.SelectedItem.ToString();
                if ((bool)CheckReverse.IsChecked)
                {
                    relname = app.SwapParts(relname);
                }
                RelNameLabel.Content = relname;
            }
        }

        private void WordSelector_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //// 处理 wordSelector 的鼠标左键释放事件
            //if (wordSelector.SelectedItem != null)
            //{
            //    RelNameLabel.Content = wordSelector.SelectedItem.ToString();
            //}
        }

        private void AddRelNameButton_Click(object sender, RoutedEventArgs e)
        {
            // 处理添加关联名称的逻辑
            // 创建对话框窗口
            Window dialog = new Window
            {
                Title = "新增关系",
                Width = 300,
                Height = 200,
                WindowStartupLocation = WindowStartupLocation.CenterScreen,
                ResizeMode = ResizeMode.NoResize,
                Owner = this, // 指定父窗口为 Edit2Rel
            };

            // 创建对话框内容
            StackPanel panel = new StackPanel { Margin = new Thickness(10) };

            System.Windows.Controls.Label label1 = new System.Windows.Controls.Label { Content = "输入一对表达关系的名字" };
            TextBox textBox1 = new TextBox { Margin = new Thickness(0, 10, 0, 10) };
            TextBox textBox2 = new TextBox { Margin = new Thickness(0, 10, 0, 10) };

            StackPanel buttonPanel = new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Right };
            Button okButton = new Button { Content = "确定", Width = 75, Margin = new Thickness(5, 0, 0, 0) };
            Button cancelButton = new Button { Content = "取消", Width = 75, Margin = new Thickness(5, 0, 0, 0) };

            buttonPanel.Children.Add(okButton);
            buttonPanel.Children.Add(cancelButton);

            panel.Children.Add(label1);
            panel.Children.Add(textBox1);
            panel.Children.Add(textBox2);
            panel.Children.Add(buttonPanel);

            dialog.Content = panel;

            // 定义变量来接受输入框的值
            string inputValue1 = string.Empty;
            string inputValue2 = string.Empty;

            // 处理按钮点击事件
            okButton.Click += (s, args) =>
            {
                // 在这里处理输入框的值
                inputValue1 = textBox1.Text;
                inputValue2 = textBox2.Text;
                if (!string.IsNullOrEmpty(inputValue1) && !string.IsNullOrEmpty(inputValue2))
                {
                    if(inputValue1 == inputValue2)
                    {
                        MessageBox.Show("两个名字不能相同", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        // 关闭对话框
                        dialog.DialogResult = true;
                        dialog.Close();
                    }
                }
                else
                {
                    MessageBox.Show("请输入有效的名称", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            };

            cancelButton.Click += (s, args) =>
            {
                dialog.DialogResult = false;
                dialog.Close();
            };

            // 显示对话框
            if (dialog.ShowDialog() == true)
            {
                var app = (App)Application.Current;
                app.AddRelName(inputValue1, inputValue2);
                var sel = wordSelector.SelectedItem;
                wordSelector.ItemsSource = app.GetRelTypes();
                wordSelector.SelectedItem = sel;
            }
        }

        private void DelRelNameButton_Click(object sender, RoutedEventArgs e)
        {
            if (wordSelector.SelectedItem != null)
            {
                var app = (App)Application.Current;
                //解析SelectedItem为|间隔的两个名字
                string[] names = wordSelector.SelectedItem.ToString().Split('|');
                if (names.Length >= 2)
                {
                    //删除关联
                    app.DelRelName(names[0], names[1]);
                    wordSelector.ItemsSource = app.GetRelTypes();
                    RelNameLabel.Content = "";
                }
            }
        }

        private void CheckReverse_Checked(object sender, RoutedEventArgs e)
        {
            if (wordSelector.SelectedItem != null)
            {
                var app = (App)Application.Current;
                var relname = wordSelector.SelectedItem.ToString();
                relname = app.SwapParts(relname);
                RelNameLabel.Content = relname;
            }
        }
        private void CheckReverse_Unchecked(object sender, RoutedEventArgs e)
        {
            if (wordSelector.SelectedItem != null)
            {
                var app = (App)Application.Current;
                var relname = wordSelector.SelectedItem.ToString();
                RelNameLabel.Content = relname;
            }
        }
    }
}
